package array;

import java.util.Arrays;

public class ArraysUtility {
    public static void main(String[] args) {
        int[] numbers = {3, 1, 4, 1, 5, 9};
        String[] words = {"apple", "banana", "cherry"};

        // 1. 数组转字符串
        System.out.println("numbers数组: " + Arrays.toString(numbers));
        System.out.println("words数组: " + Arrays.toString(words));

        // 2. 排序
        Arrays.sort(numbers);
        System.out.println("排序后: " + Arrays.toString(numbers));

        // 3. 二分查找（数组必须先排序）
        int index = Arrays.binarySearch(numbers, 5);
        System.out.println("数字5的位置: " + index);

        // 4. 填充数组
        int[] filled = new int[5];
        Arrays.fill(filled, 7); // 将所有元素填充为7
        System.out.println("填充后的数组: " + Arrays.toString(filled));

        // 5. 比较数组
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        int[] c = {1, 2, 4};
        System.out.println("a和b是否相等: " + Arrays.equals(a, b)); // true
        System.out.println("a和c是否相等: " + Arrays.equals(a, c)); // false

        // 6. 复制数组
        int[] copied = Arrays.copyOf(numbers, 4);
        System.out.println("复制的数组: " + Arrays.toString(copied));

        // 7. 数组转为列表
        java.util.List<String> wordList = Arrays.asList(words);
        System.out.println("数组转列表: " + wordList);
    }
}
